package com.romadan.core.wolfram;

import com.wolfram.jlink.Expr;
import com.wolfram.jlink.KernelLink;
import com.wolfram.jlink.MathLinkException;

public class WolframPerfomer {
	private KernelLink kernel = null;

	public WolframPerfomer() {
		FactoryKernelLink factory = new FactoryKernelLink();
		kernel = factory.getKernel();
	}

	public Expr evaluateExpr(String command) {
		Expr result = null;
		try {
			kernel.evaluate(command);
			kernel.waitForAnswer();
			result = kernel.getExpr();
		} catch (MathLinkException e) {
			System.out
					.println("[X] Exception in execution! /n Check command for kernel.exe");
			e.printStackTrace();
		}
		return result;
	}

	public void importPackage(String packages) {
		try {
			kernel.evaluate("<<\"" + packages + "\"");
			kernel.discardAnswer();
		} catch (MathLinkException e) {
			System.out
					.println("[X] Exception in import! /n Check packages and paths for kernel.exe");
			e.printStackTrace();
		}
	}

	public void close() {
		kernel.close();
	}

	protected void finalize() {
		kernel.close();
	}
}
